package com.snail.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snail.model.dto.*;
import com.snail.model.pojo.SysTeam;
import com.snail.model.pojo.SysTeamUser;
import com.snail.model.vo.SysTeamPageListVo;
import com.snail.response.BaseResponse;
import com.snail.response.PageResponse;
import com.snail.response.ResultUtils;
import com.snail.service.SysTeamService;
import com.snail.service.SysTeamUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 蔡徐坤
 * @Date 2025/7/23 16:23
 * @Description: 战队管理控制层
 */
@RestController
@RequestMapping("team")
public class SysTeamController {

    @Autowired
    private SysTeamService sysTeamService;

    @Autowired
    private SysTeamUserService sysTeamUserService;

    /**
     * 查询自己创造的战队信息通过userId
     */
    @GetMapping("/findCreateTeam")
    public BaseResponse<List<SysTeam>> findCreateTeam() {
        Long userId = StpUtil.getLoginIdAsLong();
        return ResultUtils.success(sysTeamService.findCreateTeam(userId));
    }

    /**
     * 查询自己是队长的战队信息通过userId
     */
    @GetMapping("/findMyTeams")
    public BaseResponse<List<SysTeam>> findMyTeams() {
        Long userId = StpUtil.getLoginIdAsLong();
        return ResultUtils.success(sysTeamService.selectMyTeams(userId));
    }

    /**
     * 查询自己加入的战队信息通过userId
     */
    @GetMapping("/findJoinedTeams")
    public BaseResponse<List<SysTeam>> findJoinedTeams() {
        Long userId = StpUtil.getLoginIdAsLong();
        return ResultUtils.success(sysTeamService.selectJoinedTeams(userId));
    }

    /**
     * 查询自己所订阅的全部战队信息
     */
    @GetMapping("/findSubscribeTeams")
    public BaseResponse<List<SysTeam>> findSubscribeTeams() {
        Long userId = StpUtil.getLoginIdAsLong();
        return ResultUtils.success(sysTeamService.selectSubscribeTeams(userId));
    }

    /**
     * 获取战队的详情信息
     */
    @GetMapping("/getTeamDetails")
    public BaseResponse<TeamDetailsDTO> getTeamDetails(@RequestParam Long teamId) {
        return ResultUtils.success(sysTeamService.getTeamDetails(teamId));
    }

    /**
     * 获取战队成员列表
     */
    @GetMapping("/getTeamMembers")
    public BaseResponse<List<TeamMembersDTO>> getTeamMembers(@RequestParam Long teamId) {
        return ResultUtils.success(sysTeamService.selectTeamMembers(teamId));
    }

    /**
     * 退出战队(如果是队长退出，则先转让队长，如果没有成员则直接解散战队)
     */
    @DeleteMapping("/quitTeam")
    public BaseResponse<Boolean> quitTeam(@RequestParam Long teamId) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean quitTeam = sysTeamService.quitTeam(userId, teamId);
        return ResultUtils.success(quitTeam);
    }

    /**
     * 转让队长
     */
    @PutMapping("/transferTeamLeader")
    public BaseResponse<Boolean> transferTeamLeader(@RequestParam Long teamId, @RequestParam Long newLeaderId) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean transferTeamLeader = sysTeamService.transferTeamLeader(teamId, userId, newLeaderId);
        return ResultUtils.success(transferTeamLeader);
    }

    /**
     * 修改战队详情的基本信息
     */
    @PutMapping("/updateTeamDetails")
    public BaseResponse<Boolean> updateTeamDetails(@RequestParam Long teamId, @RequestBody TeamDetailsDTO teamDetailsDTO) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean updateTeamInfo = sysTeamService.updateTeamDetails(teamId, teamDetailsDTO, userId);
        return ResultUtils.success(updateTeamInfo);
    }


    /**
     * 移除战队成员
     */
    @DeleteMapping("/removeTeamMember")
    public BaseResponse<Boolean> removeTeamMember(@RequestParam Long teamId, @RequestParam Long memberId) {
        boolean removeTeamMember = sysTeamService.removeTeamMember(teamId, memberId);
        return ResultUtils.success(removeTeamMember);
    }


    /**
     * 获取分页战队列表
     */
    @GetMapping("/getTeamList")
    public PageResponse<SysTeam> getTeamList(SysTeamPageListVo sysTeamPageListVo) {
        return sysTeamService.getTeamList(sysTeamPageListVo);

    }

    /**
     * 获取分页的带队长信息的战队列表
     */
    @GetMapping("/getTeamListWithCaptain")
    public PageResponse<SysTeamWithCaptainDTO> getTeamListWithCaptain(SysTeamPageListVo vo) {
        return sysTeamService.getTeamListWithCaptain(vo);
    }

    @PostMapping("/createTeam")
    public BaseResponse<SysTeam> createTeam(@RequestBody SysTeam sysTeam) {
        return sysTeamService.createTeam(sysTeam);

    }

    /**
     * 获取所有待审核成员的成员列表
     */
    @GetMapping("/getReviewMembers")
    public BaseResponse<List<TeamMembersDTO>> getReviewMembers(@RequestParam Long teamId) {
        Long userId = StpUtil.getLoginIdAsLong();
        List<TeamMembersDTO> reviewMembers = sysTeamService.getReviewMembers(teamId, userId);
        return ResultUtils.success(reviewMembers);
    }

    /**
     * 审核成员入队申请
     */
    @PutMapping("/reviewMember")
    public BaseResponse<Boolean> reviewMember(@RequestParam Long teamId, @RequestParam Long memberId, @RequestParam Integer status) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean reviewMember = sysTeamService.reviewMember(teamId, memberId, status, userId);
        return ResultUtils.success(reviewMember);
    }

    /**
     * 加入战队申请
     */
    @PostMapping("/applyJoinTeam")
    public BaseResponse<Boolean> applyJoinTeam(@RequestBody SysTeamUser sysTeamUser) {
        Boolean applyJoinTeam = sysTeamUserService.applyJoinTeam(sysTeamUser);
        return ResultUtils.success(applyJoinTeam);

    }


    @GetMapping("/selectMyAllTeams")
    public BaseResponse<List<SysTeam>> selectMyAllTeams() {
        Long userId = StpUtil.getLoginIdAsLong();
        return ResultUtils.success(sysTeamService.selectMyAllTeams(userId));
    }

    /**
     * 获取战队的成员数
     */
    @GetMapping("/getTeamMembersCount")
    public BaseResponse<Page<TeamWithMemberCountDTO>> getTeamMembersCount(TeamQueryDTO queryDTO) {
        return ResultUtils.success(sysTeamService.getTeamListWithRealMemberCount(queryDTO));
    }
}
